<?php
namespace App\Common\Comp\Examine;

use App\Common\Utill\Utill;
use App\Service\CheckContentLogService;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use function AlibabaCloud\Client\json;

/**
 * [网易易盾视频内容审核类]
 * 录易盾智能审核系统（https://cms.dun.163.com），您可以在[系统管理——产品管理——查看详情——获取凭证]中获取秘钥信息，在[系统管理——业务管理]中获取业务ID。
 * 账号：  18910959985  密码：kuwo321321
 *
 */
class YiDun extends Substrate
{
    protected static $config    = [];//配置参数
    protected static $url       = '';//服务器地址

    public function __construct()
    {
        static::$config         = Config::get('ExamineConfig.YiDun');
        static::$url            = Config::get('ExamineConfig.url');;
    }


    /**
     * [textCheck 网易易盾文本监测]
     * 文本同步检测接口：https://support.dun.163.com/documents/2018041901?docId=424375611814748160&locale=zh-cn
     * @param  [type] $dataId  数据ID
     * @param  [type] $userId  用户ID
     * @param  [type] $content 检测内容
     * @param  [type] $sceneId 场景
     * @param  [type] $extraData 扩展参数
     * @return [type]          [description]
     */
    public static function textCheck($dataId,$userId,$content,$sceneId,$extraData=[])
    {
        // 请求地址
        $checkUrl   = static::$config['textCheck']['checkUrl'];
        // 秘钥信息
        $SecretId   = static::$config['textCheck']['SecretList']['SecretId'];
        $SecretKey  = static::$config['textCheck']['SecretList']['SecretKey'];
        // 业务ID信息
        $businessIdArr = static::$config['textCheck']['businessId'];
        $businessId     = $businessIdArr['default'];
        if(array_key_exists($sceneId,$businessIdArr)){
            $businessId     = $businessIdArr[$sceneId];
        }

        $token=$extraData['YDtoken'] ?? '';//易盾反作弊token
        $receiveUid=$extraData['receiveUid'] ?? '';


        $callback=json_encode(['userId'=>$userId,'dataId'=>$dataId,'sceneId'=>$sceneId,'content'=>$content]);



        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                               // 产品秘钥 id
            'businessId'    => $businessId,                             // 业务id ，由易盾内容安全服务分配，业务标识
            'timestamp'     => self::microsecond(),                     // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),                     // 随机整数
            'dataId'        => $dataId,                                 // 数据唯一标识
            'content'       => $content,                                // 用户发表内容，
            'version'       => 'v4.2',                                  // 接口版本号，可选值 v4.2
            'account'       => $userId,
            'callbackUrl'   => static::$url.'/getTextYDCheck',          // 人工审核结果回调通知到客户的URL
            'callback'      => $callback,
            'token'         => $token,
        ];
        //接受消息的用户标识
        if($receiveUid){
            $data['receiveUid']=$receiveUid;
        }
        $data['signature'] = self::genSignature($SecretKey, $data); // 签名
        // 拼装请求header
        $headers = [
            'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
        ];

        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        $res = json_decode($res,true);

        //增加记录
        $taskId=$res['result']['antispam']['taskId'] ?? 0;
        (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,1,$taskId,$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));

        $result=[];
        if($res['code'] == 200 && $res['msg'] == 'ok'){
            $checkResult = $res['result'];
            //首先判断是否是反作弊，如果是，则弹出验证码
            if(isset($checkResult['anticheat'])){
                $anticheat_action=$checkResult['anticheat']['action'] ?? 0;
                if($anticheat_action != 0){
                    //检测是否有验证缓存，如果没有则弹出验证码
                    $isCode=(new CheckContentLogService())->isVerificationCode($userId,$sceneId);
                    if($isCode){
                        $result = ['code' => 91004,'message' => '存在不合规内容 请调整'];
                        return $result;
                    }
                }
            }
            if ($checkResult['antispam']['action'] == 2 || $checkResult['antispam']['action'] == 1) {
            // 建议动作，2：建议删除，1：建议审核，0：建议通过
                $result = ['code' => 91002,'message' => '存在不合规内容 请调整'];
            }

            if ($checkResult['antispam']['action'] == 0) {
            // 建议动作，2：建议删除，1：建议审核，0：建议通过
                $result = ['code' => 200,'message' => '数据一切正常'];
            }
        }else{
            $result = ['code' => 200,'message' => '数据检测请求异常'];
        }

        return $result;
    }



    /**
     * 批量文本检测
     * 批量文本检测接口：https://support.dun.163.com/documents/2018041901?docId=424382077801385984
     * @param  [type] $dataId  数据ID
     * @param  [type] $userId  用户ID
     * @param  [type] $content 检测内容
     * @param  [type] $sceneId 场景
     * @return [type]          [description]
     */
    public static function textBatchCheck($dataId,$userId,$content,$sceneId)
    {
        // 请求地址
        $checkUrl   = static::$config['textCheck']['batchCheckUrl'];
        // 秘钥信息
        $SecretId   = static::$config['textCheck']['SecretList']['SecretId'];
        $SecretKey  = static::$config['textCheck']['SecretList']['SecretKey'];
        // 业务ID信息
        $businessIdArr = static::$config['textCheck']['businessId'];
        $businessId     = $businessIdArr['default'];
        if(array_key_exists($sceneId,$businessIdArr)){
            $businessId     = $businessIdArr[$sceneId];
        }


        $_content=[];
        foreach($content as $key=>$value){
            $callback=json_encode(['userId'=>$userId,'dataId'=>$dataId,'sceneId'=>$sceneId,'content'=>$value]);
            $_data = [];
            $_data['dataId']    = $key;
            $_data['content']   = $value;
            $_data['title']     = $value;
            $_data['callbackUrl'] = static::$url.'/getTextYDCheck';
            $_data['callback']    = $callback;
            $_data['account']    = $userId;
            $_content[]         =$_data;
        }


        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                           // 产品秘钥 id
            'businessId'    => $businessId,                         // 业务id ，由易盾内容安全服务分配，业务标识
            'timestamp'     => self::microsecond(),                 // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),                 // 随机整数
            'texts'         => json_encode($_content),                // 用户发表内容，
            'version'       => 'v4',                                // 接口版本号
            'account'       => $userId,

        ];



        $data['signature'] = self::genSignature($SecretKey,$data); // 签名
        // 拼装请求header
        $headers = [
            'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
        ];
        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        $res = json_decode($res,true);


        $result=[];
        $info = [];
        if($res['code'] == 200 && $res['msg'] == 'ok'){
            $checkResult = $res['result'];
            foreach($checkResult as $key=>$value){
                $_data=$_result=[];
                $antispam=$value['antispam'];
                //增加记录
                (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,1,$antispam['taskId'],$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));
                if($antispam['action'] == 0){
                    $result = ['code' => 200,'message' => '数据一切正常'];
                }else{
                    $result = ['code' => 91002,'message' => '存在不合规内容 请调整'];
                    $info = $result;
                }
            }
        }else{
            $result = ['code' => 200,'message' => '数据检测请求异常'];
        }
        $result = !empty($info) ? $info : $result;
        return $result;
    }

    /**
     * [imgCheck 网易易盾图片监测]
     * 图片在线检测接口：https://support.dun.163.com/documents/588434277524447232?docId=588512292354793472
     * @param  [type] $dataId  数据ID
     * @param  [type] $userId  用户ID
     * @param  [type] $content 检测内容
     * @param  [type] $sceneId 场景
     * @return [type]          [description]
     */
    public static function imgCheck($dataId,$userId,$content,$sceneId,$extraData=[])
    {
        // 请求地址
        $checkUrl = static::$config['imgCheck']['checkUrl'];
        // 秘钥信息
        $SecretId = static::$config['imgCheck']['SecretList']['SecretId'];
        $SecretKey = static::$config['imgCheck']['SecretList']['SecretKey'];
        // 业务ID信息
        $businessIdArr = static::$config['imgCheck']['businessId'];
        $businessId     = $businessIdArr['default'];
        if(array_key_exists($sceneId,$businessIdArr)){
            $businessId     = $businessIdArr[$sceneId];
        }

        $token=$extraData['YDtoken'] ?? '';//易盾反作弊token
        $receiveUid=$extraData['receiveUid'] ?? '';

        $_content=[];
        foreach($content as $key=>$value){
            $callback=json_encode(['userId'=>$userId,'dataId'=>$dataId,'sceneId'=>$sceneId,'imgUrl'=>$value,'imgKey'=>$key]);
            $_data=[];
            $_data['name']=$callback;
            $_data['type']=1;
            $_data['dataId']=$key;
            $_data['data']=$value;
            $_data['callbackUrl'] = static::$url.'/getImgYDCheck';
            $_content[]=$_data;
        }

        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                        // 产品秘钥 id
            'businessId'    => $businessId,                      // 业务id ，由易盾内容安全服务分配，业务标识
            'timestamp'     => self::microsecond(),              // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),              // 随机整数
            'dataId'        => $dataId,                  // 数据唯一标识
            'version'       => 'v5.1',                           // 接口版本号，可选值 v4.2
            'images'=>json_encode($_content),
            'account'       =>  $userId,
            'token'         =>  $token,
            'extStr1'       =>  $dataId,
        ];
        if($receiveUid){
            $data['receiveUid']=$receiveUid;
        }
        $data['signature'] = self::genSignature($SecretKey,$data); // 签名
        // 拼装请求header
        $headers = [
            'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
        ];

        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        $res = json_decode($res,true);
        $result = [];
        $info = [];
        if($res['code'] == 200 && $res['msg'] == 'ok'){
            $checkResult = $res['result'];
            foreach($checkResult as $key=>$value){
                $_data = $_result = [];
                $antispam = $value['antispam'];
                //增加记录
                (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,2,$antispam['taskId'],$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));


                //首先判断是否是反作弊，如果是，则弹出验证码
                if(isset($antispam['labels']['subLabels']['details']['anticheat']['hitType'])){
                    $hitType=$antispam['labels']['subLabels']['details']['anticheat']['hitType'] ?? 0;
                    if($hitType){
                        //检测是否有验证缓存，如果没有则弹出验证码
                        $isCode=(new CheckContentLogService())->isVerificationCode($userId,$sceneId);
                        if($isCode){
                            $result = ['code' => 91004,'message' => '存在不合规内容 请调整'];
                            return $result;
                        }
                    }
                }

                if($antispam['suggestion'] == 0){
                    $result = ['code' => 200,'message' => '数据一切正常'];
                }else{
                    $result = ['code' => 91002,'message' => '存在不合规内容 请调整'];
                    $info = $result;
                }
            }
        }else{
            $result = ['code' => 200,'message' => '数据检测请求异常'];
        }
        $result = !empty($info) ? $info : $result;
        return $result;
    }


    /**
     * [videoCheck 网易易盾视频检测]
     * 点播音视频提交接口：https://support.dun.163.com/documents/594247746924453888?docId=594604048299823104
     * @param  [type] $dataId  数据ID
     * @param  [type] $userId  用户ID
     * @param  [type] $content 检测内容
     * @param  [type] $sceneId 场景
     * @return [type]          [description]
     */
    public static function videoCheck($dataId,$userId,$content,$sceneId)
    {
        // 请求地址
        $checkUrl   = static::$config['videoCheck']['checkUrl'];
        // 秘钥信息
        $SecretId   = static::$config['videoCheck']['SecretList']['SecretId'];
        $SecretKey  = static::$config['videoCheck']['SecretList']['SecretKey'];
        if($sceneId == 18){
            $SecretId   = static::$config['videoCheck']['GroupSecretList']['SecretId'];
            $SecretKey  = static::$config['videoCheck']['GroupSecretList']['SecretKey'];
        }elseif ($sceneId == 17){
            $SecretId   = static::$config['videoCheck']['PrivateSecretList']['SecretId'];
            $SecretKey  = static::$config['videoCheck']['PrivateSecretList']['SecretKey'];
        }elseif ($sceneId == 14 || $sceneId == 35 || $sceneId == 36){
            $SecretId   = static::$config['videoCheck']['StoreSecretList']['SecretId'];
            $SecretKey  = static::$config['videoCheck']['StoreSecretList']['SecretKey'];
        }elseif ($sceneId == 57){
            $SecretId   = static::$config['videoCheck']['ShopSecretList']['SecretId'];
            $SecretKey  = static::$config['videoCheck']['ShopSecretList']['SecretKey'];
        }

        $_videoUrl  = $content['url'];
        $_userId    = $userId;
        $_title     = $content['video_title'] ?? '';
        $_images    = $content['images'] ?? '';

        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                                       // 产品秘钥 id
            'timestamp'     => self::microsecond(),                             // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),                             // 随机整数
            'dataId'        => $dataId,                                         // 数据唯一标识
            'url'           => $_videoUrl,                                      // 用户发表内容，
            'account'       => $userId,                                         // 用户唯一标识
            'title'         => $_title,                                         // 视频标题
            'content'       => $_title,                                       // 音视频简介
            'version'       => 'v2',                                            // 接口版本号，可选值 v4.2
            'callbackUrl'   => static::$url.'/getVideoYDCheck',                 // 人工审核结果回调通知到客户的URL
            'scFrequency'   => 3,                                               // 截图检测频率 默认5秒
        ];
        if (!empty($_images)) {
            $data['images']=$_images;
        }
        $data['signature'] = self::genSignature($SecretKey,$data);              // 签名
        // 拼装请求header
        $headers = [
            'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
        ];

        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        $res = json_decode($res,true);
        $taskId='';
        if ($res['code'] == 200) {
            $taskId=$res['result']['taskId'];
        }elseif ($res['code'] == 417) {
            $taskId=$res['msg'];
        }

        $result = [];
        if($taskId){
            $return =[];
            $return['taskId']=$taskId;
            //增加记录
            (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,3,$taskId,$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));

            $result = ['code' => 200,'message' => '数据一切正常','data'=>$return];
        }else{
            $result = ['code' => 200,'message' => '数据检测请求异常'];
        }

        return $result;
    }


    /**
     * [liveCheck 网易易盾直播检测]
     * 直播音视频的提交接口：https://support.dun.163.com/documents/594247746924453888?docId=600827510305206272
     * @param  [type] $dataId  数据ID
     * @param  [type] $userId  用户ID
     * @param  [type] $content 检测内容
     * @param  [type] $sceneId 场景
     * @return [type]          [description]
     */
    public static function liveCheck($dataId,$userId,$content,$sceneId)
    {
       // 请求地址
        $checkUrl = static::$config['liveCheck']['checkUrl'];
        // 秘钥信息
        $SecretId = static::$config['liveCheck']['SecretList']['SecretId'];
        $SecretKey = static::$config['liveCheck']['SecretList']['SecretKey'];
        // 业务ID信息
        $businessId     = static::$config['liveCheck']['businessId']['live'];


        $_liveUrl = $content['url'];
        $_title = $content['title'] ?? '';
        $_roomNo = $content['roomNo'] ?? '';
        $_account = $content['account'] ?? '';
        $callback=json_encode(['userId'=>$userId,'dataId'=>$dataId]);
        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                                           // 产品秘钥 id
            'businessId'    => $businessId,                                         // 产品秘钥 id
            'timestamp'     => self::microsecond(),                                 // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),                                 // 随机整数
            'dataId'        => $dataId,                                             // 数据唯一标识
            'url'           => $_liveUrl,                                           // 直播流地址
            'title'         => $_title,                                             // 标题
            'roomNo'        => $_roomNo,                                            // 直播房间号
            'account'       => $_account,                                           // 直播间ID
            'version'       => 'v3',                                                // 接口版本号，可选值 v4.2
            'callbackUrl'   => static::$url.'/getLiveYDCheck',                      // 人工审核结果回调通知到客户的URL
            'scFrequency'   => 10,                                                  // 截图检测频率 默认5秒
            'detectType'    => 1,                                                   // 0：视频与音频同时过检，1：仅过检视频，2：仅过检音频
            'callback'      =>  $callback,
        ];
        $data['signature'] = self::genSignature($SecretKey,$data);                  // 签名
        // 拼装请求header
        $headers = [
            'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
        ];
        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        $res = json_decode($res,true);

        //增加记录
        $taskId=$res['result']['taskId'] ?? 0;
        (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,4,$taskId,$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));

        $return_data=['taskId'=>$taskId,'dataId'=>$dataId];

        $result = [];
        if($res['code'] == 200 && $res['msg'] == 'ok'){
            $result = ['code' => 200,'message' => '数据一切正常','data'=>$return_data];
        }else{
            $result = ['code' => 200,'message' => '数据检测请求异常'];
        }

        return $result;
    }



    /**
     * [liveaudioCheck 网易易盾语音直播检测]
     * 直播语音直播的提交接口：https://support.dun.163.com/documents/588434426518708224?docId=600817227615772672
     * @param  [type] $dataId  数据ID
     * @param  [type] $userId  用户ID
     * @param  [type] $content 检测内容
     * @param  [type] $sceneId 场景
     * @return [type]          [description]
     */
    public static function liveaudioCheck($dataId,$userId,$content,$sceneId)
    {
       // 请求地址
        $checkUrl = static::$config['liveaudioCheck']['checkUrl'];
        // 秘钥信息
        $SecretId = static::$config['liveaudioCheck']['SecretList']['SecretId'];
        $SecretKey = static::$config['liveaudioCheck']['SecretList']['SecretKey'];
        // 业务ID信息
        $businessId     = static::$config['liveaudioCheck']['businessId']['live'];

        $_liveUrl = $content['url'];
        $_title = $content['title'] ?? '';
        $_roomNo = $content['roomNo'] ?? '';
        $_account = $content['account'] ?? '';
        $callback=json_encode(['userId'=>$userId,'dataId'=>$dataId]);
        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                                           // 产品秘钥 id
            'businessId'    => $businessId,                                         // 产品秘钥 id
            'timestamp'     => self::microsecond(),                                 // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),                                 // 随机整数
            'dataId'        => $dataId,                                             // 数据唯一标识
            'url'           => $_liveUrl,                                           // 直播流地址
            'title'         => $_title,                                             // 标题
            'roomNo'        => $_roomNo,                                            // 直播房间号
            'account'       => $_account,                                           // 直播间ID
            'version'       => 'v4',                                                // 接口版本号，可选值 v4.2
            'callbackUrl'   => static::$url.'/getLiveYDCheck',                      // 人工审核结果回调通知到客户的URL
            'callback'      =>  $callback,
        ];

        $data['signature'] = self::genSignature($SecretKey,$data);                  // 签名
        // 拼装请求header
        $headers = [
            'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
        ];
        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        $res = json_decode($res,true);
        //增加记录
        $taskId=$res['result']['taskId'] ?? 0;
        (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,4,$taskId,$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));

        $return_data=['taskId'=>$taskId,'dataId'=>$dataId];

        $result = [];
        if($res['code'] == 200 && $res['msg'] == 'ok'){
            $result = ['code' => 200,'message' => '数据一切正常','data'=>$return_data];
        }else{
            $result = ['code' => 200,'message' => '数据检测请求异常'];
        }

        return $result;
    }


    /**
     * 停止直播检测
     * @param  [type] $dataId  [description]
     * @param  [type] $userId  [description]
     * @param  [type] $content [description]
     * @param  [type] $sceneId [description]
     * @return [type]          [description]
     */
    public static function feedback($dataId,$userId,$content,$sceneId)
    {
        // 请求地址
        $checkUrl = 'http://as.dun.163.com/v1/livewallsolution/feedback';
        // 秘钥信息
        if($sceneId == 37){
            $SecretId = static::$config['liveCheck']['SecretList']['SecretId'];
            $SecretKey = static::$config['liveCheck']['SecretList']['SecretKey'];
        }elseif ($sceneId == 39){
            $SecretId = static::$config['liveWallSolution']['SecretList']['SecretId'];
            $SecretKey = static::$config['liveWallSolution']['SecretList']['SecretKey'];
        }



        $content=json_encode($content);


        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                                           // 产品秘钥 id
            'timestamp'     => self::microsecond(),                                 // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),                                 // 随机整数
            'realTimeInfoList' => $content,                            // 停止检测数据
            'version'       => 'v1'                                                // 接口版本号，可选值 v1
        ];


        $data['signature'] = self::genSignature($SecretKey,$data);                  // 签名
        // 拼装请求header
        $headers = [
            'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
        ];


        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        $res = json_decode($res,true);

        $taskId=$res['result'][0]['taskId'] ?? '';

        //增加记录
        (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,4,$taskId,$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));


        $result = [];
        if($res['code'] == 200 ){
            $result = ['code' => 200,'message' => '数据一切正常'];
        }else{
            $result = ['code' => 200,'message' => '数据检测请求异常'];
        }

        return $result;
    }



    /**
     * 停止语音直播检测
     * @param  [type] $dataId  [description]
     * @param  [type] $userId  [description]
     * @param  [type] $content [description]
     * @param  [type] $sceneId [description]
     * @return [type]          [description]
     */
    public static function liveaudioFeedback($dataId,$userId,$content,$sceneId)
    {
        // 请求地址
        $checkUrl = 'http://as.dun.163.com/v1/liveaudio/feedback';
        $SecretId = static::$config['liveaudioCheck']['SecretList']['SecretId'];
        $SecretKey = static::$config['liveaudioCheck']['SecretList']['SecretKey'];
        $businessId     = static::$config['liveaudioCheck']['businessId']['live'];

        $content=json_encode($content);

        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                                           // 产品秘钥 id
            'businessId'    =>  $businessId,
            'timestamp'     => self::microsecond(),                                 // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),                                 // 随机整数
            'feedbacks' => $content,                            // 停止检测数据
            'version'       => 'v1'                                                // 接口版本号，可选值 v1
        ];


        $data['signature'] = self::genSignature($SecretKey,$data);                  // 签名
        // 拼装请求header
        $headers = [
            'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
        ];


        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        $res = json_decode($res,true);

        $taskId=$res['result'][0]['taskId'] ?? '';

        //增加记录
        (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,4,$taskId,$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));


        $result = [];
        if($res['code'] == 200 ){
            $result = ['code' => 200,'message' => '数据一切正常'];
        }else{
            $result = ['code' => 200,'message' => '数据检测请求异常'];
        }

        return $result;
    }




    /**
     * [videoSync 网易易盾视频数据抄送]
     * @param  [type] $dataId  数据ID
     * @param  [type] $userId  用户ID
     * @param  [type] $content 检测内容
     * @param  [type] $sceneId 场景
     * @return [type]          [description]
     */
    public static function videoSync($dataId,$userId,$content,$sceneId)
    {
        // 请求地址
        $checkUrl   = static::$config['videoSync']['checkUrl'];
        // 秘钥信
        $SecretId   = static::$config['videoSync']['SecretList']['SecretId'];
        $SecretKey  = static::$config['videoSync']['SecretList']['SecretKey'];

        if($sceneId == 62){
            $SecretId = static::$config['videoSync']['ShopSecretList']['SecretId'];
            $SecretKey = static::$config['videoSync']['ShopSecretList']['SecretKey'];
        }

        $_videoUrl  = $content['url'];
        $_userId    = $userId;
        $_title     = $content['video_title'] ?? '';
        $_images    = $content['images'] ?? '';

        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                                       // 产品秘钥 id
            'timestamp'     => self::microsecond(),                             // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),                             // 随机整数
            'dataId'        => $dataId,                                         // 数据唯一标识
            'url'           => $_videoUrl,                                      // 用户发表内容，
            'account'       => $userId,                                         // 用户唯一标识
            'title'         => $_title,                                         // 视频标题
            'version'       => 'v2',                                            // 接口版本号，可选值 v4.2
            'callbackUrl'   => static::$url.'/getVideoYDLabel',                 // 人工审核结果回调通知到客户的URL
            'scFrequency'   => 5,                                               // 截图检测频率 默认5秒
        ];
        if (!empty($_images)) {
            $data['images']=$_images;
        }
        $data['signature'] = self::genSignature($SecretKey,$data);              // 签名
        // 拼装请求header
        $headers = [
            'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
        ];

        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        $res = json_decode($res,true);
        $taskId='';
        if ($res['code'] == 200) {
            $taskId=$res['result']['taskId'];
        }elseif ($res['code'] == 417) {
            $taskId=$res['msg'];
        }

        $result = [];
        if($taskId){
            $return =[];
            $return['taskId']=$taskId;
            //增加记录
            (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,3,$taskId,$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));

            $result = ['code' => 200,'message' => '数据一切正常','data'=>$return];
        }else{
            $result = ['code' => 200,'message' => '数据检测请求异常'];
        }

        return $result;
    }



    /**
     * 语音审核
     * @param  [type] $dataId  [description]
     * @param  [type] $userId  [description]
     * @param  [type] $content [description]
     * @param  [type] $sceneId [description]
     * @return [type]          [description]
     */
    public static function audioCheck($dataId,$userId,$content,$sceneId)
    {
        // 请求地址
        $checkUrl   = static::$config['audioCheck']['checkUrl'];
        // 秘钥信息
        $SecretId   = static::$config['audioCheck']['SecretList']['SecretId'];
        $SecretKey  = static::$config['audioCheck']['SecretList']['SecretKey'];

        // 业务ID信息
        $businessIdArr = static::$config['audioCheck']['businessId'];
        $businessId     = $businessIdArr['default'];
        if(array_key_exists($sceneId,$businessIdArr)){
            $businessId     = $businessIdArr[$sceneId];
        }

        $_audioUrl  = $content['url'];
        $_userId    = $userId;

        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                               // 产品秘钥 id
            'businessId'    => $businessId,                             // 业务id ，由易盾内容安全服务分配，业务标识
            'timestamp'     => self::microsecond(),                     // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),                     // 随机整数
            'dataId'        => $dataId,                                 // 数据唯一标识
            'url'           => $_audioUrl,                              // 语音地址
            'account'       => $_userId,                                //  用户唯一
            'version'       => 'v2',                                    // 接口版本号，可选值 v4.2
        ];

        $data['signature'] = self::genSignature($SecretKey, $data); // 签名
        // 拼装请求header
        $headers = [
            'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
        ];

        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        $res = json_decode($res,true);


        //增加记录
        $taskId=$res['result']['antispam']['taskId'] ?? 0;
        (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,5,$taskId,$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));


        $result=[];
        if($res['code'] == 200 && $res['msg'] == 'ok'){
            $checkResult = $res['result'];
            if ($checkResult['antispam']['suggestion'] == 2 || $checkResult['antispam']['suggestion'] == 1) {
                $result = ['code' => 91002,'message' => '存在不合规内容 请调整'];
            }

            if ($checkResult['antispam']['suggestion'] == 0) {
            // 建议动作，2：建议删除，1：建议审核，0：建议通过
                $result = ['code' => 200,'message' => '数据一切正常'];
            }
        }else{
            $result = ['code' => 200,'message' => '数据检测请求异常'];
        }

        return $result;
    }


    /**
     * 付费视频检测
     * https://support.dun.163.com/documents/292111666669342720?docId=494332708285657088
     * @param  [type] $dataId  [description]
     * @param  [type] $userId  [description]
     * @param  [type] $content [description]
     * @param  [type] $sceneId [description]
     * @return [type]          [description]
     */
    public static function liveWallSolution($dataId,$userId,$content,$sceneId){
        // 请求地址
        $checkUrl   = static::$config['liveWallSolution']['checkUrl'];
        // 秘钥信息
        $SecretId = static::$config['liveWallSolution']['SecretList']['SecretId'];
        $SecretKey = static::$config['liveWallSolution']['SecretList']['SecretKey'];

        $_liveUrl = $content['url'];
        $_account = $content['account'] ?? '';
        $_key = $content['key'] ?? '';
        $callback=json_encode(['account'=>$_account,'userId'=>$userId]);
        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                                           // 产品秘钥 id
            'timestamp'     => self::microsecond(),                                 // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),                                 // 随机整数
            'dataId'        => $dataId,                                          // 数据唯一标识
            'url'           => $_liveUrl,                                           // 直播流地址
            'account'       => $_account,                                           // 直播间ID
            'detectType'    =>  1,                                                  //机器过检类型，0：直播视频与直播音频同时过检，1：仅过检直播视频，不填写默认为0
            'version'       => 'v2.1',                                                // 接口版本号
            'callbackUrl'   => static::$url.'/getliveWallSolution',                      // 人工审核结果回调通知到客户的URL
            'callback'      => $callback,
            'scFrequency'   => 5,                                                  // 截图检测频率 默认5秒
        ];
        $data['signature'] = self::genSignature($SecretKey,$data);                  // 签名
        // 拼装请求header
        $headers = [
            'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
        ];
        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        $res = json_decode($res,true);

        //增加记录
        $taskId=$res['result']['taskId'] ?? 0;
        (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,4,$taskId,$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));

        $return_data=['taskId'=>$taskId,'dataId'=>$dataId,'account'=>$_account,'key'=>$_key];

        $result = [];
        if($res['code'] == 200 && $res['msg'] == 'ok'){
            $result = ['code' => 200,'message' => '数据一切正常','data'=>$return_data];
        }else{
            $result = ['code' => 200,'message' => '数据检测请求异常'];
        }

        return $result;

    }


    /**
     * 反作弊
     * https://support.dun.163.com/documents/15588071870066688?docId=43469647845183488
     * @param  [type] $dataId  [description]
     * @param  [type] $userId  [description]
     * @param  [type] $content [description]
     * @param  [type] $sceneId [description]
     * @return [type]          [description]
     */
    public static function antiCheating($dataId,$userId,$content,$sceneId){
        // 请求地址
        $checkUrl   = static::$config['antiCheating']['checkUrl'];
        // 秘钥信息
        $SecretId = static::$config['antiCheating']['SecretList']['SecretId'];
        $SecretKey = static::$config['antiCheating']['SecretList']['SecretKey'];
        // 业务ID信息
        $businessIdArr = static::$config['antiCheating']['businessId'];
        $businessId     = $businessIdArr['default'];
        if(array_key_exists($sceneId,$businessIdArr)){
            $businessId     = $businessIdArr[$sceneId];
        }

        $token=$content['token'] ?? '';
        $account=$content['account'] ?? '';
        $phone=$content['account'] ?? '';
        $channel=$content['channel'] ?? '';
        $ip=$content['ip'] ?? '';
        if(empty($ip)){
            $ip=get_real_ip() ?? '';      // 客户端真实ip地址
        }

        $callback=json_encode(['token'=>$token,'account'=>$account,'ip'=>$ip]);
        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                                           // 产品秘钥 id
            'businessId'    => $businessId,                             // 业务id ，由易盾内容安全服务分配，业务标识
            'timestamp'     => self::microsecond(),                                 // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),                                 // 随机整数
            'version'       => '300',
            'token'         =>  $token,               //token
            'account'       =>  $userId,             //用户唯一标识，如果是手机号or邮箱，支持传入hash值，hash算法：md5(account)
            'phone'         =>  md5($phone),               //手机号
            'ip'            =>  $ip                   //用户IP

        ];

        if($channel){
            $data['activityId']=$channel;
        }

        $data['signature'] = self::genSignature($SecretKey,$data);                  // 签名
        // 拼装请求header
        $headers = [
            'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
        ];
        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        Log::info("antiCheatingRes:".json_encode($res,JSON_UNESCAPED_UNICODE));
        $res = json_decode($res,true);
        //增加记录
        $data['content']=$content;


        $result = ['code' => 200,'message' => '数据一切正常'];
        if($sceneId == 47 || $sceneId == 48){
            return $result;
        }else{
            if($res['code'] == 200 && $res['msg'] == 'ok'){
                $taskId=$res['result']['taskId'] ?? 0;
                (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,6,$taskId,$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));
                if($res['result']['action'] == 0 || $res['result']['action'] == 10){
                        $result = ['code' => 200,'message' => '数据一切正常'];
                    }else{
                        //检测是否有验证缓存，如果没有则弹出验证码
                        $isCode=(new CheckContentLogService())->isVerificationCode($userId,$sceneId);
                        if($isCode){
                            $result = ['code' => 91004,'message' => '存在不合规内容 请调整'];
                            return $result;
                        }
                    }

            }else{
                $result = ['code' => 200,'message' => '数据检测请求异常'];
            }

            return $result;

        }
    }





    /**
     * 生成签名信息
     * $secretKey 产品私钥
     * $params 接口请求参数，不包括signature参数
     */
    private static function genSignature($secretKey,$params){
        ksort($params);
        $buff="";
        foreach($params as $key=>$value){
            $buff .=$key;
            $buff .=$value;
        }
        $buff .= $secretKey;
        return md5(mb_convert_encoding($buff, "utf8", "auto"));
    }

    /**
     * 获取毫秒
     * @return [type] [description]
     */
    private static function microsecond()
    {
        $t = explode(" ", microtime());
        $microsecond = round(round($t[1].substr($t[0],2,3)));
        return $microsecond;
    }


    /**
     * 行为式验证码
     * https://support.dun.163.com/documents/15588062143475712?docId=69218161355051008
     * @param  [type] $dataId  [description]
     * @param  [type] $userId  [description]
     * @param  [type] $content [description]
     * @param  [type] $sceneId [description]
     * @return [type]          [description]
     */
    public static function verificationCode($dataId,$userId,$content,$sceneId){
        // 请求地址
        $checkUrl   = static::$config['verificationCode']['checkUrl'];
        // 秘钥信息
        $SecretId = static::$config['verificationCode']['SecretList']['SecretId'];
        $SecretKey = static::$config['verificationCode']['SecretList']['SecretKey'];

        $captchaId=$content['captchaId'] ?? '';
        $validate=$content['validate'] ?? '';

        // 拼装请求内容
        $data = [
            'secretId'      => $SecretId,                                           // 产品秘钥 id
            'timestamp'     => self::microsecond(),                                 // 请求当前 UNIX 时间戳 毫秒
            'nonce'         => rand(100000,999999),                                 // 随机整数
            'version'       => 'v2',
            'captchaId'     =>  $captchaId,                             //验证码id
            'validate'      =>  $validate,                              //提交二次校验的验证数据
            'user'          =>  $userId                                //用户标识
        ];

        $data['signature'] = self::genSignature($SecretKey,$data);                  // 签名
        // 拼装请求header
        $headers = [
            'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8',
        ];
        // 发送请求
        $res = Utill::httpRequest($checkUrl, 'POST', $data, $headers, false);
        $res = json_decode($res,true);

        //增加记录
        (new CheckContentLogService())->AddCheckContenLog(1,$sceneId,7,time(),$userId,$dataId,json_encode($data,JSON_UNESCAPED_UNICODE),json_encode($res,JSON_UNESCAPED_UNICODE));

        if($res['result'] == 'true'){
            $result = ['code' => 200,'message' => '数据一切正常'];
        }else{
            $result = ['code' => 200,'message' => $res['msg']];
        }
        return $result;

    }

}
